package com.chat.view;

import com.chat.Local;
import com.chat.service.UpDateUserMessage;
import com.chat.io.ChangeImage;
import com.chat.utils.IsMail;
import com.chat.io.SetCircle;
import javafx.event.ActionEvent;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDate;

public class UserMessageController {

    public DatePicker born;
    public TextField nicknameTextField;
    public TextField mailboxTextField;
    public TextArea signTextField;
    public Label usernamelabel;
    public Label tishilabel;
    public Button Return;
    public Label nicknamelabel;
    public Label borndatelabel;
    public Label mailboxlabel;
    public String Sex;//保存性别
    public RadioButton malebutton;
    public RadioButton femalebutton;
    public ImageView headpicture;
    public Button xiugaihead;
    public File file=null;

    public void initialize(){//初始化个人信息
          nicknameTextField.setText(Local.user.getNickname());
          SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
          born.setValue(LocalDate.parse(dateFormat.format(Local.user.getBorndate())));
          if(Local.user.getSex().equals("male"))
              malebutton.setSelected(true);
          else femalebutton.setSelected(true);
          usernamelabel.setText(Local.user.getUsername());
          mailboxTextField.setText(Local.user.getMailbox());
          if(Local.user.getQianming()!=null)
          signTextField.setText(Local.user.getQianming());

        File file1=new File("D:/chatproject/client/"+Local.user.getUsername()+".png");
        headpicture.setImage(new Image("file:"+file1.getAbsolutePath()));
        headpicture.setClip(new SetCircle().getRectangle(40,40));
    }


    public void xiugai(ActionEvent event) {
          int flag=1;
        if(IsMail.isEmail(mailboxTextField.getText().toString()))
        {
            flag++;
            mailboxlabel.setText("邮箱有效");
        }
        else mailboxlabel.setText("邮箱无效");
          if(malebutton.isSelected()==true)
              Sex="male";
          else Sex="female";
        if(flag==2) {
             Alert alert = new Alert(Alert.AlertType.INFORMATION);
         alert.setTitle("提示");
         alert.setHeaderText("恭喜");
         alert.setContentText("登录成功！");
         alert.showAndWait();
          Local.user.setSex(Sex);
          String birthday=born.getValue().toString();
          Local.user.setBorndate(Date.valueOf(birthday));
          Local.user.setMailbox(mailboxTextField.getText());
          Local.user.setQianming(signTextField.getText());
          Local.user.setNickname(nicknameTextField.getText());
          if(file!=null) {
              ChangeImage ci = new ChangeImage();
              byte[] b = ci.get(file);
              ci.set(Local.user.getUsername(), b);
              Local.user.setHeadpicture(b);
          }
        UpDateUserMessage.updateusermessage();
        }
        else tishilabel.setText("修改失败");
    }

    public void fanhui(ActionEvent event) {
        Stage stage=(Stage)Return.getScene().getWindow();
        stage.close();
        try {
            MainWindowView.start(new Stage());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void headOnAction(ActionEvent event) {
        FileChooser chooser=new FileChooser();
        chooser.setInitialDirectory(new File("D:\\chatproject\\headimage"));
        chooser.setTitle("选择图片");
        chooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Image Files","*.png"));
         file=chooser.showOpenDialog(Return.getScene().getWindow());
        if(file==null){
            System.out.println("未选择");
        }
        else{
            System.out.println("成功选择");
            System.out.println(file.toURI().toString());
            System.out.println(file.getAbsoluteFile());
            headpicture.setImage(new Image("file:"+file.getAbsolutePath()));
        }
    }
}
